package com.iscreen.utils.exceptions;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class IScreenException extends Exception
{

	private static final long serialVersionUID = -8752657221446987087L;

	private final static Logger logger = Logger
			.getLogger(IScreenException.class);

	private final static int DEFAULT_CODE = IScreenExceptionCodes.UNKNOWN_ERROR;
	private final static Level DEFAULT_LEVEL = Level.ERROR;

	private int code;

	public IScreenException(Exception e)
	{
		this(e, e.getMessage());
	}

	public IScreenException(Exception e, String logMessage)
	{
		this(DEFAULT_CODE, e, logMessage);
	}

	public IScreenException(int code, Exception e)
	{
		this(code, e, e.getMessage());
	}

	public IScreenException(int code, Exception e, String logMessage)
	{
		super(logMessage, e);
		this.code = code;
	}

	public IScreenException(int code, String logMessage)
	{
		super(logMessage);
		this.code = code;
	}

	public int getCode()
	{
		return code;
	}

	public void log()
	{
		// TODO should check whether it's best to log by default or not
		// I decided not to write logs whenever an exception is thrown,
		// because some exceptions may not necessarily mean real errors (for
		// instance, when duplicate entries are identified when inserting to
		// DB).
		// In any case, there should be a central point which catches this
		// exception, and this point will log the exception
		logger.log(DEFAULT_LEVEL, getMessage(), this);
	}
	
	public void log(Level logLevel)
	{
		logger.log(logLevel, getMessage(), this);
	}
}
